package com.liuliuliu.payment.globalpaymentspringbootstarter.context;

import com.liuliuliu.payment.globalpaymentspringbootstarter.enums.PaymentChannelEnum;
import com.liuliuliu.payment.globalpaymentspringbootstarter.request.GetPaymentLinkReq;
import com.liuliuliu.payment.globalpaymentspringbootstarter.response.GenPaymentLinkResp;
import com.liuliuliu.payment.globalpaymentspringbootstarter.service.PaymentService;
import com.liuliuliu.payment.globalpaymentspringbootstarter.service.impl.CommercePaymentService;
import com.liuliuliu.payment.globalpaymentspringbootstarter.service.impl.StripePaymentService;
import lombok.extern.slf4j.Slf4j;


import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;


public class PaymentContext {

    private static Logger log = Logger.getLogger(PaymentContext.class.getName());

    private static  Map<PaymentChannelEnum, PaymentService> paymentChannelServiceMap = new HashMap<>();

    static {
        paymentChannelServiceMap.put(PaymentChannelEnum.COMMERCE, new CommercePaymentService());
        paymentChannelServiceMap.put(PaymentChannelEnum.STRIPE, new StripePaymentService());

        for (PaymentChannelEnum paymentChannelEnum : paymentChannelServiceMap.keySet()) {

            log.info("加载支付策略:" + paymentChannelEnum.name());
        }
    }

    public static GenPaymentLinkResp genPaymentUrl(GetPaymentLinkReq req) {
        PaymentService service = paymentChannelServiceMap.get(req.getPaymentChannel());
        if (service == null) {
            log.info("payment channel not exist");
            return null;
        }
        return service.genPaymentUrl(req);
    }

}
